# condin=utf-8
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


class Email:
    smtp = "";
    user = ""
    passd = ""
    send = ""

    # 初始化类
    def __init__(self, smtp="smtp.qq.com", user="1412485108@qq.com", passd="xuahcjqacdkrhjde",
                 send="1412485108@qq.com"):
        self.user = user
        self.passd = passd
        self.smtp = smtp
        self.send = send

    # 构建邮件内容。文本邮件 内容  发送人 接收人 标题
    def TextEmail(self, text, fro, to, title):
        massage = MIMEText(text, 'plain', 'utf-8')
        massage['From'] = Header(fro, 'utf-8')
        massage['To'] = Header(to, 'utf-8')
        massage['Subject'] = Header(title, 'utf-8')
        return massage
        pass

    # 构建带附件的邮件正文
    def FileEmail(self, text, fro, to, title):
        message = MIMEMultipart()
        message['From'] = Header(fro, 'utf-8')
        message['To'] = Header(to, 'utf-8')
        message['Subject'] = Header(title, 'utf-8')
        return message
        pass

    #  excekl,txt 等文文本型附近构建
    def ExcelEmail(self, filepath, filename):
        att1 = MIMEText(open(filepath, 'rb').read(), 'base64', 'utf-8')
        att1["Content-Type"] = 'application/octet-stream'
        # 这里的filename可以任意写，写什么名字，邮件中显示什么名字
        att1.add_header('Content-Disposition', 'attachment', filename=filename)
        return att1
        pass

    # 发送邮件     构建的邮件主题  发件人{数组}
    def sendEmali(self, massage, emails):
        smt = smtplib.SMTP_SSL(host=self.smtp)
        smt.connect(self.smtp, 465)
        smt.login(self.user, self.passd)
        smt.sendmail(self.send, emails, massage.as_string())
        pass

# if __name__ == '__main__':
#     em=Email();
#     # msg=em.TextEmail("我的测试邮件","python脚本","gaozhigang","python的邮件标题")
#     # em.sendEmali(msg,["1412485108@qq.com","17611584521@163.com"])
#     # print("邮件发送完成")
#     msg = em.FileEmail("我的测试邮件", "python脚本", "gaozhigang", "python的邮件标题")
#     fj=em.ExcelEmail(r"D:\offer_date\root\luckygold.xls","我的小可爱.xls")
#     msg.attach(fj)
#     em.sendEmali(msg,["1412485108@qq.com","17611584521@163.com"])
#     print("邮件发送完成")
